package com.gxa.service.shezhiservice.Impl;

import com.gxa.common.R;
import com.gxa.mapper.shezhimapper.GongYingShangMapper;
import com.gxa.pojo.GongYingShang;
import com.gxa.pojo.GongYingShangType;
import com.gxa.service.shezhiservice.GongYingShangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author weishen
 */
@Service
public class GongYingShangServiceImpl implements GongYingShangService {

    @Autowired
    public GongYingShangMapper gongYingShangMapper;

    R r = new R();

    @Override
    public R selectAllGongYingShang() {
        r.setCode(200);
        r.setMsg("查询成功！");
        List<GongYingShang> gongYingShangs = gongYingShangMapper.selectAllGongYingShang();
        r.setCount(gongYingShangMapper.selectCountGongYingShang());
        r.setData(gongYingShangs);
        return r;
    }

    @Override
    public int selectCountGongYingShang() {
        return gongYingShangMapper.selectCountGongYingShang();
    }

    @Override
    public R selectAllGongYingShangForPage(int page, int size) {
        r.setCode(200);
        r.setMsg("查询成功！");
        int index = (page - 1)*size;
        List<GongYingShang> gongYingShangs = gongYingShangMapper.selectAllGongYingShangForPage(index, size);
        r.setCount(gongYingShangMapper.selectCountGongYingShang());
        r.setData(gongYingShangs);
        return r;
    }

    @Override
    public R selectAllGongYingShangType() {
        r.setCode(200);
        r.setMsg("查询成功");
        List<GongYingShangType> gongYingShangTypes = gongYingShangMapper.selectAllGongYingShangType();
        r.setData(gongYingShangTypes);
        return r;
    }



    @Override
    public R selectGongYingShangByTypeOrBianHaoOrLianXiRenOrMingChenOrPhone(int page, int size, String gongYingShangType
            , String gongYingShangBianHao, String gongYingShangMingCheng, String lianXiRen, String phone) {
        r.setCode(200);
        r.setMsg("查询成功！");
        int index = (page - 1)*size;
        List<GongYingShang> gongYingShangs = gongYingShangMapper.selectGongYingShangByTypeOrBianHaoOrLianXiRenOrMingChenOrPhone
                (index,size,gongYingShangType,gongYingShangBianHao,gongYingShangMingCheng,lianXiRen,phone);
        r.setCount(selectCountAnyGongYingShang(gongYingShangType,gongYingShangBianHao,gongYingShangMingCheng,lianXiRen,phone));
        r.setData(gongYingShangs);
        return r;
    }

    @Override
    public int selectCountAnyGongYingShang(String gongYingShangType
            , String gongYingShangBianHao, String gongYingShangMingCheng, String lianXiRen, String phone) {
        return gongYingShangMapper.selectCountAnyGongYingShang(gongYingShangType,gongYingShangBianHao,gongYingShangMingCheng,lianXiRen,phone);
    }

    @Override
    public R insertGongYingShang(GongYingShang gongYingShang) {
        int res = gongYingShangMapper.insertGongYingShang(gongYingShang);
        r.setCode(500);
        r.setMsg("查询失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("查询成功");
        }
        return r;
    }

    @Override
    public R updateGongYingShangById(GongYingShang gongYingShang) {
        int res = gongYingShangMapper.updateGongYingShangById(gongYingShang);
        r.setCode(500);
        r.setMsg("更新失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("更新成功");
        }
        return r;
    }

    @Override
    public R deleteGongYingShangById(ArrayList<Integer> id) {
        int res=0;
        int flag=1;
        for(int i=0;i<id.size();i++)
        {
            res=gongYingShangMapper.deleteGongYingShangById(id.get(i));
            if(res==0)
            {
                flag=0;
                break;
            }
        }
        r.setCode(200);
        r.setMsg("删除成功");
        if(flag==0)
        {
            r.setCode(500);
            r.setMsg("删除失败");
        }
        return r;
    }
}
